<template>
	<view class="second-content">
		<view class="second-box">
			<view class="second-top">
				<view class="top-image">
					<image :src="courseDetail.full_cover" mode=""></image>
				</view>
				<view class="top-detail">
					<view class="second-name">{{courseDetail.title}}</view>
					<view class="second-context">
						<view>￥{{courseDetail.price}}</view>
						<view>{{courseDetail.class}}课时</view>
					</view>
				</view>
			</view>
			<view class="second-video">
<!-- 				<view class="video-title">
					<view>视频</view>
				</view> -->
				<u-tabs :list="list" active-color='#2AC092' :is-scroll="false" :current="current" @change="change"></u-tabs>
				<view>
					<!-- 简介 -->
					<view style="padding:20rpx;" v-if="current==0">
						<view v-html="courseDetail.description"></view>
					</view>
					<!-- 视频 -->
					<view class="video-list" v-else>
						<view class="video-item" v-for="item in videoList" :key="item.id" @click="learnCourse(courseDetail.id)">
							<view>
								<!-- <image :src="item.image" mode=""></image> -->
								<video id="myVideo" :src="item.full_video" :poster="item.full_cover" @play="isBuy(courseDetail.id)"
								object-fit="fill" enable-danmu danmu-btn controls></video>
							</view>
							<view>{{item.title}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="sub-btn">
				<u-button shape="circle" :custom-style="customStyle" @click="gotoDetail(courseDetail.id)">立即购买</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import { getvideoDetails } from '../../api/index.js';
	import { createOrderDY, } from '../../api/pay.js';
import { toast } from '../../utils/common.js';
	export default {
		data() {
			return {
				customStyle: {
					background: '#2AC092',
					color: '#fff'
				},
				videoList: [],
				courseDetail:{},
				// 
				pid:'',
				
				list:[
					{
						name: '简介',
						id:0,
					}, {
						name: '视频',
						id:1,
					}
				],
				current:0,
			}
		},
		onLoad(options) {
			this.pid=options.id||''
		},
		onShow(){
			if(this.pid){
				this.getvideoDetail(this.pid)
			}
		},
		methods: {
			//切换
			change(val){
				this.current=val
			},
			gotoDetail(id){
				let that = this
				if (uni.getStorageSync('access_token') != '') {
					createOrderDY({video_course_id:id}).then(res=>{
						toast('订单创建成功')
						uni.navigateTo({
							url:'/pagesA/confirmOrder/index?order_no='+ res.data.data.order_no + '&productInfo=' + encodeURIComponent(JSON.stringify(that.courseDetail))
						})
					}).catch()
				} else {
					uni.showModal({
						title: '提示',
						content: '您当前未登录，是否登录？',
						success: function (res) {
							if (res.confirm) {
								that.gotoLigin()
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			learnCourse(id) {
				let that = this
				let videoContext = uni.createVideoContext('myVideo');
				videoContext.pause();
				if(courseDetail.is_pay==1){
					videoContext.play()
				}else{
					uni.showModal({
						content: '请先购买课程，才可以播放',
						cancelText: '暂不购买',
						cancelColor: '#666666',
						confirmText: '购买课程',
						confirmColor: '#2AC092',
						success: function(res) {
							if (res.confirm) {
								that.gotoDetail(id);
							}
						}
					})
				}
			},
			getvideoDetail(id){
				let that = this
				getvideoDetails(id).then(res=>{
					// console.log(985,res.data.data);
					that.courseDetail = res.data.data;
					that.videoList = res.data.data.videos
				}).catch()
			},
			gotoLigin() {
				uni.navigateTo({
					url: '/pages/login/index'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.second-content{
		padding-bottom:120rpx;
	}
	.second-top {
		.top-image {
			width: 100%;
			height: 380rpx;


			>image {
				width: 100%;
				height: 100%;
			}
		}

		.top-detail {
			background-color: #fff;
			padding: 20rpx;

			.second-name {
				color: #313233;
				font-size: 34rpx;
			}

			.second-context {
				margin-top: 20rpx;
				display: flex;
				justify-content: space-between;

				>view:nth-child(1) {
					color: #FF4D4D;
					font-size: 36rpx;
				}

				>view:nth-child(2) {
					color: #999999;
					font-size: 26rpx;
				}
			}
		}
	}

	.second-video {
		margin-top: 20rpx;

		.video-title {
			background-color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;

			>view {
				height: 100%;
				border-bottom: 4rpx solid #2AC092;
				padding: 20rpx 10rpx;
			}
		}

		.video-list {
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			padding: 20rpx;

			.video-item {
				width: 49%;
				background-color: #fff;
				border-radius: 10rpx;
				margin-bottom: 20rpx;

				>view:nth-child(1) {
					height: 200rpx;
					width: 100%;

					#myVideo {
						border-top-left-radius: 10rpx;
						border-top-right-radius: 10rpx;
						width: 100%;
						height: 100%;
					}
				}

				>view:nth-child(2) {
					padding: 20rpx;
					font-size: 28rpx;
				}
			}
		}
	}

	.sub-btn {
		position: fixed;
		bottom: 5rpx;
		width: 100%;
		padding: 20rpx;
		background-color: #fff;
		height:120rpx;
	}
</style>